<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>申购单列表---申请人</title>
    <script type="text/javascript">
      var purchaseId;
      var purchaseDS;
      var purchaseForVendorWin;
      Ext.Msg.getDialog().setWidth(400);
      //打开窗口，准备发布。
      function openPurchaseWindow(id,searchType){
        //竞价之前先查看，是否已经竞价过。
        if ( searchType === "bidding"){
          Ext.Ajax.request({
            url : '/purchase/isBid?id=' + id,
            success : function (response, req) {
              if (Ext.util.JSON.decode(response.responseText).info === "success") {
                definePurchaseWin(id,searchType);
              } else {
                Ext.MessageBox.alert('提示', '您已经参与过此产品的竞价，谢谢.');
              }
            },
            failure : function (result, request){
            },
            method : 'POST'
          });
        }else{
          definePurchaseWin(id,searchType);
        }
      }

      //统一打开窗口
      function definePurchaseWin(id,searchType){
        purchaseForVendorWin = new Ext.Window({
          layout: 'fit',
          width: 825,
          height: 460,
          closeAction: 'destroy',
          plain: true,
          modal: true,
          constrain:true,
          //modal: true,
          title: '申购单详细信息',
          autoLoad: {
            url: "/purchase/purchaseForVendor?id="+id+"&search_type="+searchType,
            scripts: true
          }
        });
        purchaseForVendorWin.on("destroy",function(){
          purchaseDS.reload();
        });
        purchaseForVendorWin.resizable = false;
        purchaseForVendorWin.show();
      }
      

      Ext.onReady(function(){
        //searchForm
        var searchType = new Ext.form.Hidden({
          name: 'search_type',
          value: '<%=params[:search_type]%>'
        });
        
        var purchaseNo = new Ext.form.TextField({
          fieldLabel: '申购单号',
          allowBlank: true,
          blankText: "申购单位不能为空",
          name: 'order_number',
          anchor: '96%'
        });

        var productName = new Ext.form.TextField({
          fieldLabel: '产品名称',
          allowBlank: true,
          blankText: "产品名称",
          name: 'product_name',
          anchor: '96%'
        });

        var unitPriceFrom = new Ext.form.NumberField({
          fieldLabel: '单价(从)',
          name: 'unit_price_from',
          allowBlank: true,
          allowDecimals: false,
          allowNegative: false,
          anchor: '95%'
        });

        var unitPriceTo = new Ext.form.NumberField({
          fieldLabel: '到',
          name: 'unit_price_to',
          allowBlank: true,
          allowDecimals: false,
          allowNegative: false,
          anchor: '95%'
        });

        var amountFrom = new Ext.form.NumberField({
          fieldLabel: '总额(从)',
          name: 'amount_from',
          allowBlank: true,
          allowDecimals: false,
          allowNegative: false,
          anchor: '95%'
        });

        var amountTo = new Ext.form.NumberField({
          fieldLabel: '到',
          name: 'amount_to',
          allowBlank: true,
          allowDecimals: false,
          allowNegative: false,
          anchor: '95%'
        });
        
        //搜索按钮
        var searchButton = new Ext.Button({
          text: '搜索',
          tooltip: '搜索申购记录',
          iconCls: 'finding',
          handler: function(){
            purchaseDS.baseParams.search_type = searchType.getValue();
            purchaseDS.baseParams.product_name = productName.getValue();
            purchaseDS.baseParams.unit_price_from = unitPriceFrom.getValue();
            purchaseDS.baseParams.unit_price_to = unitPriceTo.getValue();
            purchaseDS.baseParams.amount_from = amountFrom.getValue();
            purchaseDS.baseParams.amount_to = amountTo.getValue();            
            purchaseDS.load({
              params: {
                start: 0,
                limit: 15
              }
            });
          }
        })
        
        var searchPurchaseForm = new Ext.FormPanel({
          labelWidth: 55, // label settings here cascade unless overridden
          url:'/purchase/create',
          frame:true,
          labelAlign: 'right',
          buttonAlign: 'right',
          //title: '添加申购单',
          bodyStyle:'padding:5px 5px 0',
          baseCls:'x-panel-noborder',
          width: 720,
          //height:300,
          items: [
            {
              xtype: 'fieldset',
              defaults: {width: 710},
              title: '条件搜索',
              border: true,
              items: [
                {
                  layout: 'column',
                  border: false,
                  items: [{
                      columnWidth: .3,
                      layout: 'form',
                      border: false,
                      items: [purchaseNo]
                    }, {
                      columnWidth: .3,
                      layout: 'form',
                      border: false,
                      items: [productName]
                    }]
                },{
                  layout: 'column',
                  border: false,
                  items: [{
                      columnWidth: .15,
                      layout: 'form',
                      border: false,
                      items: [unitPriceFrom]
                    },{
                      columnWidth: .15,
                      layout: 'form',
                      border: false,
                      items: [unitPriceTo]
                    },{
                      columnWidth: .15,
                      layout: 'form',
                      border: false,
                      items: [amountFrom]
                    },{
                      columnWidth: .15,
                      layout: 'form',
                      border: false,
                      items: [amountTo]
                    },{
                      columnWidth: .2,
                      layout: 'form',
                      border: false,
                      items: [searchType]
                    },{
                      columnWidth: .2,
                      layout: 'form',
                      border: false,
                      items: [searchButton]
                    }]
                }]
            }]
        });
        //产品列表---------------------------------------------------------
        //生成有关客户的Grid
        purchaseDS = new Ext.data.Store({
          paramNames: {
            start: "start",
            limit: "limit"
          },
          baseParams: {
            start: 0,
            limit: 15
          },
          proxy: new Ext.data.HttpProxy({
            url: '/purchase/getPurchasesForVendor?search_type=<%=params[:search_type]%>'
          }),
          reader: new Ext.data.JsonReader({
            totalProperty: 'totalProperty',
            root: 'root'
          }, [{
              name: 'id'
            },{
              name: 'manager'
            }, {
              name: 'product_name'
            }, {
              name: 'model'
            }, {
              name: 'specification'
            }, {
              name: 'manufacturer'
            }, {
              name: 'begin_date'
            }, {
              name: 'end_date'
            }, {
              name: 'auditing_status'
            }, {
              name: 'release_status'
            }, {
              name: 'brand'
            },{
              name: 'supply_date'
            }])
        });
        var purchaseSM = new Ext.grid.CheckboxSelectionModel();
        var purchaseCM = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(),purchaseSM,{
            header: '产品名称',
            dataIndex: 'product_name',
            width:180
          }, {
            header: '品牌',
            dataIndex: 'brand',
            width:60
          }, {
            header: '型号规格',
            dataIndex: 'specification',
            width:80
          }, {
            header: '生产商',
            dataIndex: 'manufacturer',
            width:90
          }, {
            header: '供货日期',
            dataIndex: 'supply_date',
            renderer: Ext.util.Format.dateRenderer('Y-m-d'),
            width:75
          }, {
            header: '竞开日期',
            dataIndex: 'begin_date',
            renderer: Ext.util.Format.dateRenderer('Y-m-d'),
            width:75
          }, {
            header: '竞束日期',
            dataIndex: 'end_date',
            renderer: Ext.util.Format.dateRenderer('Y-m-d'),
            width:75
          }, {
            header: '审批状态',
            dataIndex: 'auditing_status',
            renderer:auditingRender,
            width:60
          }, {
            header: '发布状态',
            dataIndex: 'release_status',
            width:60
          }, {
            header: '操作',
            dataIndex: 'manufacturer',
            renderer: actionRender,
            width:50
          }]);

        function actionRender(value, cellmeta, record, rowIndex, columnIndex, store){
          if(searchType.getValue() === "all"){
            return
          }else if(searchType.getValue() === "bidding"){
            return "<a style=cursor:pointer;color:blue onclick=openPurchaseWindow(" + store.getAt(rowIndex).get('id')+",'"+searchType.getValue()+"')>竞价</a>";
          }else if(searchType.getValue() === "participation"||searchType.getValue() === "win"||searchType.getValue() === "failure"){
            return "<a style=cursor:pointer;color:blue onclick=openPurchaseWindow(" + store.getAt(rowIndex).get('id')+",'"+searchType.getValue()+"')>查看</a>";
          }
        }

        var purchaseGrid =  new Ext.grid.GridPanel({
          ds: purchaseDS,
          cm: purchaseCM,
          sm: new Ext.grid.RowSelectionModel({singleSelect:true}),
          width: screenWidth-230,
          height: screenHeight-400,
          iconCls: 'icon-grid',loadMask: true,
          loadMask: true,
          bbar: new Ext.PagingToolbar({
            pageSize: pageSize,
            store: purchaseDS,
            displayInfo: true,
            displayMsg: "显示第 {0} 条到 {1} 条记录，一共 {2} 条",
            emptyMsg: "没有记录"
          })
        });
        purchaseDS.load();

        //总的panel
        var workbenchPanel = new Ext.Panel({
          width: screenWidth-220,
          height: screenHeight-260,
          //html: '&lt;empty panel&gt;',
          //cls:'cellpadding:10',
          /*layout: {
            type:'vbox',
            align:'stretch'
          },*/
          frame:true,
          layout:'table',
          layoutConfig: {columns:1},
          defaults:{margins:'5 5 5 5'},
          //bodyStyle: 'padding:15px;align:center',
          //listeners: {
          //expand: showAgileDiagram
          //},
          items:[searchPurchaseForm,purchaseGrid]
        });
        workbenchPanel.render("workbench_panel");
      })
    </script>
  </head>
  <body>
    <div id="search_form"></div>
    <div id="purchase_list"></div>
    <div id="workbench_panel"></div>
  </body>
</html>
